/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-01-20
 * Time: 16:06
 */
public class MyLinkedList {

    public class ListNode {
        private int val;
        private ListNode pre;
        private ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
    }
    ListNode head = null;
    ListNode last = null;
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }

    }

    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = last = node;
        }else {
            last.next = node;
            node.pre = last;
            last = node;
        }
    }

    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if (head == null) {
            head = last = node;
        }else {
            cur.pre = node;
            node.next = cur;
            head =  node;
        }
    }
    public void indexOf(int index,int data) {
        ListNode cur = head;
        ListNode node = new ListNode(data);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        while(index > 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        node.pre = cur.pre;
        cur.pre.next = node;
        cur.pre = node;
    }
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode Next = cur.next;
            cur.next = null;
            cur.pre = null;
            cur = Next;
        }
        head = null;
        last = null;
    }
    public void delKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur.pre == null&&cur.next == null) {
                    head = null;
                    last = null;
                    return;
                }
                if (cur.pre == null) {
                    head = head.next;
                    head.pre = null;
                    return;
                }
                if (cur.next == null) {
                    last = last.pre;
                    last.next = null;
                    return;
                }
                    cur.pre.next = cur.next;
                    cur.next.pre = cur.pre;
                    return;
            }
            cur = cur.next;
        }
        }
    public void delKeyAll(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur.pre == null && cur.next == null) {
                    head = null;
                    last = null;
                    return;
                }
                if (cur.pre == null) {
                    head = head.next;
                    head.pre = null;
                }
                if (cur.next == null) {
                    last = last.pre;
                    last.next = null;
                    return;
                }
                if (cur.pre != null) {
                    cur.pre.next = cur.next;
                    cur.next.pre = cur.pre;
                }
            }
            cur = cur.next;
        }
    }
}
